package f_collection;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.SwingConstants;

public class MyCalendar extends JFrame {

	Calendar now;
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	private JPanel contentPane;
	private JLabel result;
	private JButton btnNewButton;
	private JButton btnNewButton_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyCalendar frame = new MyCalendar();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	
	public void now(){
		String temp = sdf.format(now.getTimeInMillis());
		result.setText(temp);
	}
	public MyCalendar() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getResult());
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());
		
		now = Calendar.getInstance();
		now();
		
		
	}
	private JLabel getResult() {
		if (result == null) {
			result = new JLabel("New label");
			result.setHorizontalAlignment(SwingConstants.CENTER);
			result.setBounds(12, 10, 108, 15);
		}
		return result;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("이전달");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					now.add(Calendar.MONTH, -1);
					now();
				}
			});
			btnNewButton.setBounds(132, 6, 97, 23);
		}
		return btnNewButton; 
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("다음달");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					now.add(Calendar.MONTH, 1);
					now();
				}
			});
			btnNewButton_1.setBounds(240, 6, 97, 23);
		}
		return btnNewButton_1;
	}
}
